/*
 * Copyright (c) 2013, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.storage;

import org.omorphdb.core.storage.document.DocumentDescriptor;
import org.omorphdb.core.storage.document.FieldFactory;

final class DocumentDescriptorImpl implements DocumentDescriptor
{
	DocumentStatus status;
	Long key;

	int classID;

	int attributesCount;
	long docPosition;
	int docSize;

	boolean updated;

	/**
	 * Method that provides the instance of a document descriptor from required
	 * parameters.
	 * 
	 * @param status The status of the document.
	 * @param classID The unique ID of the class the document represents.
	 * @param attrCount Number of attributes of the document.
	 * @param docPosition The position of the document within the storage file.
	 *        In case of a just created document and not yet wrote to the
	 *        storage, this value should be -1.
	 * @param docSize The size of the document, (only document not the
	 *        descriptor, in other words the size occupied by attribute names
	 *        and attribute values).
	 * @param key The unique integer identifier of the document.
	 * @return
	 */
	static DocumentDescriptorImpl define(DocumentStatus status, int classID, int attrCount, long docPosition,
			int docSize, long key)
	{
		return new DocumentDescriptorImpl(status, classID, attrCount, docPosition, docSize, key);
	}

	/**
	 * Method that provides the instance of descriptor of a document which
	 * should be just created.
	 * 
	 * @param classID The unique ID of the class the document represents.
	 * 
	 * @return
	 */
	static DocumentDescriptorImpl defineNew(int classID)
	{
		return new DocumentDescriptorImpl(DocumentStatus.NEW, classID, 0, -1, 0, null);
	}

	/**
	 * Creates a document providing this as descriptor.
	 * 
	 * @param factory
	 * 
	 * @return
	 */
	public DocumentImpl createDocument(FieldFactory factory)
	{
		return new DocumentImpl(this, factory);
	}

	/*
	 * @param status The status of the document.
	 * 
	 * @param classID a string containing class information (string with java
	 * class canonical form).
	 * 
	 * @param attrCount Number of attributes of the document.
	 * 
	 * @param docPosition The position of the document within the storage file.
	 * In case of a just created document and not yet wrote to the storage, this
	 * value should be -1.
	 * 
	 * @param docSize The size of the document, (only document not the
	 * descriptor, in other words the size occupied by attribute names and
	 * attribute values).
	 * 
	 * @param key The unique integer identifier of the document (in case the
	 * document is new it should be null)
	 */
	private DocumentDescriptorImpl(DocumentStatus status, int classID, int attrCount, long docPosition, int docSize,
			Long key)
	{
		this.status = status;
		this.key = key;

		this.updated = key != null;

		this.classID = classID;

		this.attributesCount = attrCount;

		this.docPosition = docPosition;
		this.docSize = docSize;
	}

	@Override
	public Long getDefaultKey()
	{
		return this.key;
	}

	@Override
	public DocumentStatus getDocumentStatus()
	{
		return this.status;
	}

	@Override
	public boolean isUpdated()
	{
		return this.updated;
	}

	@Override
	public int getAttributesCount()
	{
		return this.attributesCount;
	}

	@Override
	public long getDocumentPosition()
	{
		return docPosition;
	}

	@Override
	public int getDocumentSize()
	{
		return this.docSize;
	}

	@Override
	public int getClassID()
	{
		return this.classID;
	}
}
